package com.zoke.nebbox.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.zoke.neb.avatar.LogUtil;
import com.zoke.neb.avatar.api.NebAvatar;
import com.zoke.neb.avatar.http.SmartCallback;
import com.zoke.neb.avatar.model.NebFile;
import com.zoke.nebbox.Conf;
import com.zoke.nebbox.R;
import com.zoke.nebbox.adapter.NebFileAdapter;
import com.zoke.nebbox.base.BaseFragment;
import com.zoke.nebbox.model.FileResult;
import com.zoke.nebbox.tools.PersistTool;
import com.zoke.nebbox.tools.Util;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wulijie on 2018/6/8.
 */
public class MainF extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {
    @ViewInject(R.id.swipe)
    SwipeRefreshLayout swipeRefreshLayout;
    @ViewInject(R.id.recycler)
    RecyclerView recyclerView;

    private NebFileAdapter nebFileAdapter;
    private List<NebFile> mList = new ArrayList<>();

    private int page = 0;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setContentView(R.layout.f_main);
        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.baseColor));
        swipeRefreshLayout.setOnRefreshListener(this);
        nebFileAdapter = new NebFileAdapter(mList);
        nebFileAdapter.openLoadAnimation();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(nebFileAdapter);
        nebFileAdapter.setOnItemChildClickListener(this);
        nebFileAdapter.setOnLoadMoreListener(this, recyclerView);

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        swipeRefreshLayout.setRefreshing(true);
        page = 0;
        getFileList();
    }

    private void getFileList() {
        String address = PersistTool.getString(Conf.KEY, "");
        if (TextUtils.isEmpty(address)) address = Conf.DEFAULT_ADDRESS;

        NebAvatar.getInstance().getAvatarList(address, page, 10, new SmartCallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(final String response) {
                context.runOnUiThread(() -> doResult(response));
            }

            @Override
            public void onError(final String error) {
                context.runOnUiThread(() -> doError(error));
            }
        });
    }

    private void doError(String error) {
        if (page != 0) {
            nebFileAdapter.loadMoreComplete();
        } else {
            swipeRefreshLayout.setRefreshing(false);
        }
        Toast.makeText(context, "网络不给力哦:" + error, Toast.LENGTH_LONG).show();
    }

    private void doResult(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONObject resJ = jsonObject.getJSONObject("result");
            String json = resJ.optString("result");
            FileResult result = new Gson().fromJson(json, FileResult.class);
            if (result != null && result.data != null) {
                if (page == 0) {
                    mList.clear();
                    swipeRefreshLayout.setRefreshing(false);
                }
                mList.addAll(result.data);
                if (mList.size() == result.total) {
                    //没有更多了
                    nebFileAdapter.loadMoreEnd(false);
                } else {
                    nebFileAdapter.loadMoreComplete();
                }
                nebFileAdapter.notifyDataSetChanged();
            }
        } catch (JSONException e) {
            LogUtil.e(e.toString());
        }
    }


    @Override
    public void onRefresh() {
        page = 0;
        getFileList();
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        getFileList();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.tv_go:
                Util.openBrowser(context, mList.get(position).url);
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }
}
